<?php
	
	Class UserDefinedFieldBehavior extends ModelBehavior
	{
		private $__defaultSettings = array('validate'=>true,'primaryOnly'=>true);
		
		var $cacheKey = 'user_defiend_field_';
		private $UserDefinedField;
		var $settings = array();
		function setup($model, $config=array())
		{
			if(!is_object($this->UserDefinedField))
				$this->UserDefinedField = ClassRegistry::init('Users.UserDefinedField');
			$this->settings[$model->alias] = am($this->__defaultSettings,$config);
			//$model->additionalFields[]  = 'userDefinedFields';
			//$model->cacheFields["userDefinedFields"] = "onUpdate";
		}
		
		function afterSave(&$model,$created)
		{
			if(!isset($model->data['UserDefinedField']) || !is_array($model->data['UserDefinedField']) || count($model->data['UserDefinedField']) == 0)
				return;
				
			if($created !== true)
			{
				$this->UserDefinedField->deleteAll(array('item_id'=>$model->id,'type'=>$model->alias));
			}
			foreach($model->data['UserDefinedField'] as $field)
			{
				$this->addField($model,$field,$model->id);
			}
			$this->removeCacheField($model,$model->id);
		}
		
		function addField($model,$field,$item_id)
		{
			if(!isset($field['deleted']) || $field['deleted'] != 1)
			{
				$default=array('label'=>'','value'=>'');
				$data['UserDefinedField'] = am($default,$field);
				$data['UserDefinedField']  = am($data['UserDefinedField'],array('item_id'=>$item_id,'type'=>$model->alias));
				$this->UserDefinedField->create();
				$this->UserDefinedField->save($data,$this->settings[$model->alias]['validate']);	
			}
		}
		function userDefinedFields(AppModel $model,$record,$refresh=false)
		{
			if(!isset($record[$model->alias][$model->primaryKey]))
				return;
			$itemId = $record[$model->alias][$model->primaryKey];
			if($refresh==true)
				$this->removeCacheField($model,$item_id);
				$userDefinedFields = $this->getCachedField($model,$itemId);
			if(is_null($userDefinedFields)==false && $userDefinedFields!==false)
			{
					return $userDefinedFields;
			}
			$userDefinedFields = $this->UserDefinedField->find('all',array('conditions'=>array('item_id'=>$itemId,'type'=>$model->alias),'fields'=>array('label','value','id')));
			if($userDefinedFields)
			{
				$userDefinedFields = Set::combine($userDefinedFields,'{n}.UserDefinedField.id','{n}.UserDefinedField');
			}
			$this->addCacheField($model,$itemId,$userDefinedFields);
			if(is_null($userDefinedFields))
			{
				$userDefinedFields = array();
			}
			return $userDefinedFields;
		}
		
		function getCachedField($model,$itemId)
		{
			Cache::set(array('path'=>CACHE,'duration'=>"+1 day"));
			$data = Cache::read($this->getCacheKey($model,$itemId));
			return $data;
			
		}
		
		function addCacheField($model,$itemId,$data)
		{
			Gigacache::set(array('path'=>CACHE,'duration'=>"+1 day"));
			Gigacache::write($this->getCacheKey($model,$itemId),$data);
		}
		
		function removeCacheField($model,$itemId)
		{
			Gigacache::delete($this->getCacheKey($model,$itemId));
		}
		
		function afterFind($model,$results,$primary)
		{
			if(!isset($results[0][$model->alias]['id']))
				return $results;
			foreach($results as &$result)
			{
				$result[$model->alias]['userDefinedFields'] = $this->userDefinedFields($model,$result);
				if($primary && $model->recursive > -1)
					$result['UserDefinedField'] = $result[$model->alias]['userDefinedFields'];
			}
			return $results;
		}
		
		function getCacheKey($model,$itemId)
		{
			return $this->cacheKey.'_'.$model->alias.'_'.$itemId;
		}
	}
?>